package com.zuipin.controller;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zuipin.entity.product.EvaluationImg;
import com.zuipin.entity.product.EvaluationInfo;
import com.zuipin.entity.product.OverallEvaluation;
import com.zuipin.framework.annotation.Inject;
import com.zuipin.framework.annotation.Json;
import com.zuipin.framework.annotation.Jsons;
import com.zuipin.framework.annotation.Valid;
import com.zuipin.framework.annotation.Valids;
import com.zuipin.framework.emuns.ErrorCode;
import com.zuipin.framework.exception.CustomException;
import com.zuipin.framework.result.PageResult;
import com.zuipin.framework.result.Result;
import com.zuipin.service.IEvaluationInfoService;
import com.zuipin.service.IOrderService;
import com.zuipin.service.IProductExtInfoService;
import com.zuipin.util.DateUtils;
import com.zuipin.util.Pagination;
import com.zuipin.vo.EvaluationInfoVo;
import com.zuipin.vo.EvaluationVo;
import com.zuipin.vo.saveEvaluationVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 
 *
 * @ClassName EvaluationInfoController
 * @author wangcongyi
 * @date 2017年5月3日 上午11:18:55
 */
@Api(value = "评论", description = "评论信息-wangcongyi")
@Controller
@RequestMapping(value = "/evaluation")
public class EvaluationInfoController {
	
	@Resource
	private IEvaluationInfoService	evaluationInfoService;
	@Resource
	private IProductExtInfoService	productExtInfoService;
	@Resource
	private IOrderService			orderService;
	
	@Jsons({ @Json(type = EvaluationVo.class, exclude = "delFlag,orderId,overallEvaluation,phone,proTitle,state,sysId"),
			@Json(type = EvaluationImg.class, include = "id,evaluationId,imgUrl") })
	@RequestMapping(value = "/seachEvaluation", method = RequestMethod.POST)
	@ApiOperation(value = "518-分页查询评论信息(前台)-wangcongyi", notes = "分页查询评论信息")
	@Valids({ @Valid(name = "eval.proExtId", required = true), @Valid(name = "eval.sysId", required = true), @Valid(name = "page.pageNumber", required = true),
			@Valid(name = "page.pageSize", required = true), })
	@ApiImplicitParams({ @ApiImplicitParam(name = "isExistImg", value = "是否只查询有图的评价[是：1]", required = false, dataType = "short", paramType = "query"),
			@ApiImplicitParam(name = "eval.proExtId", value = "商品拓展信息ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "eval.sysId", value = "系统ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "page.pageNumber", value = "页码", required = true, dataType = "integer", paramType = "query"),
			@ApiImplicitParam(name = "page.pageSize", value = "单页记录数", required = true, dataType = "integer", paramType = "query"), })
	public Result<?> seachEvaluation(Short isExistImg, @Inject @ApiIgnore EvaluationInfo eval, @Inject @ApiIgnore Pagination page) {
		PageResult<EvaluationInfoVo> result = new PageResult<EvaluationInfoVo>();
		result.setData(evaluationInfoService.seachEvaluation(isExistImg, eval, page));
		result.setTotal_record(page.getTotalRecord());
		result.setErrorCode(ErrorCode.SUCCESS);
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value = "/saveEvaluation", method = RequestMethod.POST)
	@ApiOperation(value = "三期-用户对商品进行评价(前台)-wangcongyi", notes = "用户对商品进行评价" + "</br>返回码" + "</br>0-成功" + "</br>1001-参数错误" + "</br>1002-必填项未填写" + "</br>1004-系统异常"
			+ "</br>5101-订单已评价" + "</br>5102-至少填写一件商品评价")
	@Valids({ @Valid(name = "vo.overallEvaluation.orderId", required = true), @Valid(name = "vo.overallEvaluation.sysId", required = true),
			@Valid(name = "vo.overallEvaluation.description", minValue = 1, maxValue = 5), @Valid(name = "vo.overallEvaluation.logistics", minValue = 1, maxValue = 5),
			@Valid(name = "vo.overallEvaluation.service", minValue = 1, maxValue = 5), @Valid(name = "vo.evaluationInfos.memberId", required = true),
			@Valid(name = "vo.evaluationInfos.nickName", required = false), @Valid(name = "vo.evaluationInfos.proExtId", required = true),
			@Valid(name = "vo.evaluationInfos.content", minLength = 5, maxLength = 200),
			@Valid(name = "vo.evaluationInfos.score", required = true, minValue = 1, maxValue = 100), })
	public Result<?> saveEvaluation(@RequestBody @ApiParam saveEvaluationVo vo) throws CustomException {
		Result<Map<String, Object>> result = new Result<>();
		boolean isReview = false;
		Integer reviewNum = 0;
		// 对整体评价做必填判断
		OverallEvaluation overallEvaluation = vo.getOverallEvaluation();
		StringBuilder overallBuilder = new StringBuilder("请对");
		String sp = "";
		boolean isRequired = false;
		if (null == overallEvaluation.getDescription()) {
			overallBuilder.append("描述相符");
			sp = "/";
			isRequired = true;
		}
		if (null == overallEvaluation.getLogistics()) {
			overallBuilder.append(sp);
			overallBuilder.append("物流服务");
			sp = "/";
			isRequired = true;
		}
		if (null == overallEvaluation.getService()) {
			overallBuilder.append(sp);
			overallBuilder.append("服务态度");
			isRequired = true;
		}
		if (isRequired) {
			overallBuilder.append("进行评价");
			result.setErrorCode(ErrorCode.PARAM_ABSENT);
			result.setMessage(overallBuilder.toString());
			return result;
		}
		// 对订单是否评论过进行判断
		boolean isComment = orderService.isComment(vo.getOverallEvaluation().getOrderId());
		if (isComment) {
			result.setErrorCode(ErrorCode.ORDER_EVALUATED);
			result.setMessage("该订单已评价!");
			return result;
		}
		Set<Long> set = new HashSet<>();
		List<EvaluationInfo> evaluationInfos = vo.getEvaluationInfos();
		// 对评价进行严谨性判断
		if (null != evaluationInfos && !evaluationInfos.isEmpty()) {
			for (EvaluationInfo evaluationInfo : evaluationInfos) {
				set.add(evaluationInfo.getProExtId());
			}
			if (evaluationInfos.size() > set.size()) {
				result.setErrorCode(ErrorCode.REPEATED_EVALUATION);
				result.setMessage("请不要对同一件商品重复评价");
				return result;
			}
			for (EvaluationInfo evaluationInfo : evaluationInfos) {
				if (null == evaluationInfo.getContent()) {
					continue;
				}
				++reviewNum;
				isReview = true;
				// break;
			}
		}
		if (!isReview) {
			result.setErrorCode(ErrorCode.AT_LEAST_FILL_PRODUCT_EVALUATION);
			result.setMessage("请至少填写一件商品的评论");
			return result;
		}
		evaluationInfoService.saveEvaluation(vo, reviewNum);
		result.setErrorCode(ErrorCode.SUCCESS);
		result.setMessage("评价成功,赠送" + reviewNum * EvaluationInfo.GIFT_POINTS + "积分!");
		return result;
	}
	
	@Jsons({ @Json(type = EvaluationVo.class, include = "id,content,createTime,score,replyContent,orderId,orderNo,sysId,state,isReply,isTop,proTitle,phone,overallEvaluation,evaluationImgs"),
			@Json(type = EvaluationImg.class, include = "id,evaluationId,imgUrl"),
			@Json(type = OverallEvaluation.class, include = "id,description,logistics,service,sysId,orderId") })
	@RequestMapping(value = "/findEvaluation", method = RequestMethod.POST)
	@ApiOperation(value = "三期-分页查询评论信息(后台)-wangcongyi", notes = "分页查询评论信息" + "</br>返回码" + "</br>0-成功" + "</br>1001-参数错误" + "</br>1002-必填项未填写" + "</br>1004-系统异常")
	@Valids({ @Valid(name = "eval.proSku"), @Valid(name = "eval.account"), @Valid(name = "eval.score"), @Valid(name = "eval.orderNo"), @Valid(name = "eval.sysId", required = true),
			@Valid(name = "eval.state", required = true), @Valid(name = "eval.startTime"), @Valid(name = "eval.endTime"), @Valid(name = "eval.isReply"),
			@Valid(name = "page.pageNumber", required = true), @Valid(name = "page.pageSize", required = true), })
	@ApiImplicitParams({ @ApiImplicitParam(name = "eval.proSku", value = "商品Sku", dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "eval.account", value = "账号（手机号、邮箱或id）", dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "eval.score", value = "商品评分", dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "eval.orderNo", value = "订单编号", dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "eval.sysId", value = "系统ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "eval.state", value = "评论状态  0 待审核 1通过 2屏蔽", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "eval.startTime", value = "评论时间(prefix)", dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "eval.endTime", value = "评论时间(suffix)", dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "eval.isReply", value = "是否回复", dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "page.pageNumber", value = "页码", required = true, dataType = "integer", paramType = "query"),
			@ApiImplicitParam(name = "page.pageSize", value = "单页记录数", required = true, dataType = "integer", paramType = "query"), })
	public Result<List<EvaluationVo>> findEvaluation(@Inject @ApiIgnore EvaluationInfo eval, @Inject @ApiIgnore Pagination page) {
		PageResult<List<EvaluationVo>> result = new PageResult<>();
		result.setData(evaluationInfoService.findEvaluation(eval, page));
		result.setTotal_record(page.getTotalRecord());
		result.setErrorCode(ErrorCode.SUCCESS);
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value = "/verify", method = RequestMethod.POST)
	@ApiOperation(value = "三期-审核评论-wangcongyi", notes = "审核评论" + "</br>返回码" + "</br>0-成功" + "</br>1000-评论信息不完整" + "</br>1001-参数错误" + "</br>1002-必填项未填写" + "</br>1004-系统异常"
			+ "</br>2000-评价信息不存在")
	@Valids({ @Valid(name = "eval.id", required = true), @Valid(name = "eval.state", required = true), @Valid(name = "eval.isTop", required = true, minValue = 0, maxValue = 1), })
	@ApiImplicitParams({ @ApiImplicitParam(name = "eval.id", value = "评价信息ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "eval.state", value = "评论状态 1 通过 2屏蔽", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "eval.isTop", value = "是否置顶 0不置顶 1置顶", required = true, dataType = "long", paramType = "query"), })
	public Result<Boolean> verify(@Inject @ApiIgnore EvaluationInfo eval) {
		Result<Boolean> result = new Result<>();
		EvaluationInfo evaluationInfo = evaluationInfoService.selectEvaluationInfo(eval);
		if (null != evaluationInfo) {
			Long orderId = evaluationInfo.getOrderId();
			if (null != orderId) {
				eval.setOrderId(evaluationInfo.getOrderId());
			} else {
				result.setErrorCode(ErrorCode.ELSE);
				result.setMessage("评价信息不完整！");
				return result;
			}
		} else {
			result.setErrorCode(ErrorCode.NOT_FIND);
			result.setMessage("评价信息不存在！");
			return result;
		}
		// 业务逻辑
		if (EvaluationInfo.PASS.equals(eval.getState()) || EvaluationInfo.SHIELD.equals(eval.getState())) {
		} else {
			result.setErrorCode(ErrorCode.ELSE);
			result.setMessage("请正确传输状态值！");
			return result;
		}
		boolean flag = evaluationInfoService.verify(eval);
		result.setData(flag);
		result.setErrorCode(ErrorCode.SUCCESS);
		result.setMessage("审核成功");
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value = "/reply", method = RequestMethod.POST)
	@ApiOperation(value = "三期-评论回复-wangcongyi", notes = "评论回复" + "</br>返回码" + "</br>0-成功" + "</br>1001-参数错误" + "</br>1002-必填项未填写" + "</br>1004-系统异常")
	@Valids({ @Valid(name = "eval.id", required = true), @Valid(name = "eval.replyPerson", required = true, maxLength = 100),
			@Valid(name = "eval.replyContent", required = true, minLength = 5, maxLength = 200) })
	@ApiImplicitParams({ @ApiImplicitParam(name = "eval.id", value = "评价信息ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "eval.replyPerson", value = "回复者", required = true, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "eval.replyContent", value = "回复内容", required = true, dataType = "string", paramType = "query"), })
	public Result<Boolean> reply(@Inject @ApiIgnore EvaluationInfo eval) {
		Result<Boolean> result = new Result<>();
		eval.setIsReply(EvaluationInfo.IS_REPLY);
		eval.setReplyTime(DateUtils.getCurrentDateTime());
		boolean flag = evaluationInfoService.reply(eval);
		result.setData(flag);
		result.setErrorCode(ErrorCode.SUCCESS);
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value = "/getReplyContent", method = RequestMethod.GET)
	@ApiOperation(value = "三期-获取评论回复内容-wangcongyi", notes = "获取评论回复内容" + "</br>返回码" + "</br>0-成功" + "</br>1000-评论信息不完整" + "</br>1001-参数错误" + "</br>1002-必填项未填写" + "</br>1004-系统异常"
			+ "</br>2000-评价信息不存在")
	@Valids({ @Valid(name = "eval.id", required = true), })
	@ApiImplicitParams({ @ApiImplicitParam(name = "eval.id", value = "评价信息ID", required = true, dataType = "long", paramType = "query"), })
	public Result<String> getReplyContent(@Inject @ApiIgnore EvaluationInfo eval) {
		Result<String> result = new Result<>();
		EvaluationInfo evaluationInfo = evaluationInfoService.selectEvaluationInfo(eval);
		if (null != evaluationInfo) {
			if (null != evaluationInfo.getReplyContent()) {
				result.setData(evaluationInfo.getReplyContent());
			}
		}
		result.setErrorCode(ErrorCode.SUCCESS);
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value = "/getNoEvaluatedNum", method = RequestMethod.GET)
	@ApiOperation(value = "获取待审核的评价数量", notes = "获取待审核的评价数量")
	@Valids({ @Valid(name = "sysId", required = true), })
	@ApiImplicitParams({ @ApiImplicitParam(name = "sysId", value = "系统ID", required = true, dataType = "long", paramType = "query"), })
	public Result<Integer> getNoEvaluatedNum(@RequestParam(value = "sysId") Long sysId) {
		Result<Integer> result = new Result<>();
		int count = evaluationInfoService.getNoEvaluatedNum(sysId);
		result.setData(count);
		result.setErrorCode(ErrorCode.SUCCESS);
		return result;
	}
	
}
